<?php

	class CleanVarnishAction extends CommonAction{
		public  function index(){
			$this->display();
		}
		
		public  function CleanUp(){
			if($_GET){
				$domain = $_GET['domain'];
				$imgurl = $_GET['clean_url'];
			}
			
			if($_POST){
				$domain = $_POST['domain'];
				$imgurl = $_POST['clean_url'];
			}
			
			$squid = array(
				'172.16.83.111',
				'172.16.83.110',
			); 
			$flag=false;
			
						
			foreach($squid as $ip){
				$result=$this->purge($ip, $domain, $imgurl);
				if(!empty($ip)){
					if(strpos($result,"200")){
						$flag=true;
					}else{
						$flag=false;
						echo json_encode(array("msg"=>"fail"));
						exit;
					}
				}
			}
			if($flag){
				echo json_encode(array("msg"=>"clear","res"=>$result));
			}else{
				echo json_encode(array("msg"=>"fail"));
			}
		}
		
		public  function SelectUp(){
			if($_POST){
				foreach($_POST as $key=>$val){
					if($key=="home"){
						$cleanUrl['home'][]="http://www.stylehouse.com.cn/*";
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/*";
					}else if($key=="is_home"){
						$cleanUrl['home'][]="http://www.stylehouse.com.cn/*";
					}else if($key=="is_home_js"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/public/js/*";
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/tpl/home/js/*";
					}else if($key=="is_home_css"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/tpl/home/css/*";
					}else if($key=="is_home_img"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/public/upload/share/*";
						$cleanUrl['fashion'][]="http://s.stylehouse.com.cn/tpl/home/images/*";
					}else if($key=="is_home_xml"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/public/upload/adv/*";
					}else if($key=="is_home_flash"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/tpl/home/swf/*";
					}else if($key=="auto"){
						$cleanUrl['auto'][]="http://www.styleauto.com.cn/*";
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/*";
					}else if($key=="is_auto"){
						$cleanUrl['auto'][]="http://www.styleauto.com.cn/*";
					}else if($key=="is_auto_js"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/public/js/*";
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/tpl/auto/js/*";
					}else if($key=="is_auto_img"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/public/upload/share/*";
						$cleanUrl['fashion'][]="http://s.styleauto.com.cn/tpl/auto/images/*";
					}else if($key=="is_auto_css"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/tpl/auto/css/*";
					}else if($key=="is_auto_xml"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/public/upload/adv/*";
					}else if($key=="is_auto_flash"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/tpl/auto/swf/*";
					}else if($key=="fashion"){
						$cleanUrl['fashion'][]="http://www.trendstyle3d.com/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/*";
					}else if($key=="is_fashion"){
						$cleanUrl['fashion'][]="http://www.trendstyle3d.com/*";
					}else if($key=="is_fashion_js"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/js/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/js/*";
					}else if($key=="is_fashion_img"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/upload/share/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/images/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/images/*";
					}else if($key=="is_fashion_css"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/css/*";
					}else if($key=="is_fashion_xml"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/upload/adv/*";
					}else if($key=="is_fashion_flash"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/swf/*";
					}
				}
			}
			if($_GET){
				foreach($_GET as $key=>$val){
					if($key=="home"){
						$cleanUrl['home'][]="http://www.stylehouse.com.cn/*";
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/*";
					}else if($key=="is_home"){
						$cleanUrl['home'][]="http://www.stylehouse.com.cn/*";
					}else if($key=="is_home_js"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/public/js/*";
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/tpl/home/js/*";
					}else if($key=="is_home_css"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/tpl/home/css/*";
					}else if($key=="is_home_img"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/public/upload/share/*";
						$cleanUrl['fashion'][]="http://s.stylehouse.com.cn/tpl/home/images/*";
					}else if($key=="is_home_xml"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/public/upload/adv/*";
					}else if($key=="is_home_flash"){
						$cleanUrl['home'][]="http://s.stylehouse.com.cn/tpl/home/swf/*";
					}else if($key=="auto"){
						$cleanUrl['auto'][]="http://www.styleauto.com.cn/*";
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/*";
					}else if($key=="is_auto"){
						$cleanUrl['auto'][]="http://www.styleauto.com.cn/*";
					}else if($key=="is_auto_js"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/public/js/*";
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/tpl/auto/js/*";
					}else if($key=="is_auto_img"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/public/upload/share/*";
						$cleanUrl['fashion'][]="http://s.styleauto.com.cn/tpl/auto/images/*";
					}else if($key=="is_auto_css"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/tpl/auto/css/*";
					}else if($key=="is_auto_xml"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/public/upload/adv/*";
					}else if($key=="is_auto_flash"){
						$cleanUrl['auto'][]="http://s.styleauto.com.cn/tpl/auto/swf/*";
					}else if($key=="fashion"){
						$cleanUrl['fashion'][]="http://www.trendstyle3d.com/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/*";
					}else if($key=="is_fashion"){
						$cleanUrl['fashion'][]="http://www.trendstyle3d.com/*";
					}else if($key=="is_fashion_js"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/js/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/js/*";
					}else if($key=="is_fashion_img"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/upload/share/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/images/*";
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/images/*";
					}else if($key=="is_fashion_css"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/css/*";
					}else if($key=="is_fashion_xml"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/public/upload/adv/*";
					}else if($key=="is_fashion_flash"){
						$cleanUrl['fashion'][]="http://s.trendstyle3d.com/tpl/fashion/swf/*";
					}
				}
			}

			$squid = array(
				'172.16.83.111',
				'172.16.83.110',
			); 
			$flag=false;
			foreach($squid as $ip){
				if(!empty($ip)){
					foreach($cleanUrl as $key=>$val){
						if($key=="home"){
							$domain="http://www.stylehouse.com.cn";
						}else if($key=="auto"){
							$domain="http://www.styleauto.com.cn";
						}else if($key=="fashion"){
							$domain="http://www.trendstyle3d.com";
						}
						$result=$this->purge($ip, $domain, $val);
						if(strpos($result,"200")){
							$pass=true;
						}else{
							$pass=false;
						}
						if($pass){
							$flag=true;
						}else{
							$flag=false;
							echo json_encode(array("msg"=>"fail"));
							exit;
						}
					}
				}
			}
			if($flag){
				echo json_encode(array("msg"=>"clear","res"=>$result));
			}else{
				echo json_encode(array("msg"=>"fail"));
			}
		}
				
		
		public function purge($ip, $domain, $url){
			$errstr = '';
			$errno = '';
			$fp = fsockopen ($ip, 80, $errno, $errstr, 2);
			if (!$fp){
				return false;
			}else{
				/* $out = 'ban req.url ' . $url;
                fputs($fp, $out);
                $out = fgets($fp, 4096);
                fclose($fp);
                return true; */
				/* $out = "purge.url $url \r\n";
				fputs ($fp, $out);
				$out = fgets($fp , 4096);
				fclose ($fp);
				return $out; */
				
				$out = "PURGE $url HTTP/1.1\r\n";
				$out .= "Host:$domain\r\n";
				$out .= "Connection: close\r\n\r\n";
				fputs ($fp, $out);
				$out = fgets($fp , 4096);
				fclose ($fp);
				return $out;
			}
		}
		
	}